package com.qining618.cms.cms.service.impl;

import com.qining618.cms.cms.domain.ContentRecord;
import com.qining618.cms.cms.enums.ContentOperateEnum;
import com.qining618.cms.cms.mapper.ContentRecordMapper;
import com.qining618.cms.cms.service.ContentRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qining618.common.exception.BusinessException;
import com.qining618.common.result.ErrorCode;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 * 内容操作记录 服务实现类
 * </p>
 *
 * @author qining
 * @date 2019-09-20
 */
@Transactional(readOnly = true, rollbackFor = Exception.class)
@Service
public class ContentRecordServiceImpl extends ServiceImpl<ContentRecordMapper, ContentRecord> implements ContentRecordService {

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ContentRecord save(Long contentId, Long userId, ContentOperateEnum operate) {
        ContentRecord contentRecord = new ContentRecord();
        contentRecord.setContentId(contentId).setUserId(userId).setOperateType(operate).setOperateTime(new Date());
        int opNum = this.baseMapper.insert(contentRecord);
        if (opNum != 1) {
            throw new BusinessException(ErrorCode.DB_ERROR);
        }
        return contentRecord;
    }

    @Override
    public boolean hasContentUp(Long contentId, Long userId) {
        return this.baseMapper.existByContentIdAndUserIdAndOperateType(contentId, userId, ContentOperateEnum.UP);
    }

    @Override
    public boolean hasContentDown(Long contentId, Long userId) {
        return this.baseMapper.existByContentIdAndUserIdAndOperateType(contentId, userId, ContentOperateEnum.DOWN);
    }
}
